package com.asion.controller;

import com.asion.domain.Detail;
import com.asion.domain.Video;
import com.asion.mapper.DetailMapper;
import com.asion.mapper.VideoMapper;
import com.asion.vo.VideoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private DetailMapper detailMapper;
    @RequestMapping("/duanjuList")
    private IPage<VideoVo> duanjuList(Integer pageNo, Integer pageSize, String keyWord){
        QueryWrapper<Video> queryWrapper = null;
        MPJLambdaWrapper<Detail> detailMPJLambdaWrapper =  new MPJLambdaWrapper<Detail>()
                .selectAll(Detail.class)
                .selectAsClass(Video.class,VideoVo.class)
                .leftJoin(Video.class, Video::getId,Detail::getId);
        if(!StringUtils.isNullOrEmpty(keyWord)  && keyWord.length()>0){
            queryWrapper = new QueryWrapper<Video>().like("vod_name",keyWord);
            detailMPJLambdaWrapper =  new MPJLambdaWrapper<Detail>()
                    .selectAll(Detail.class)
                    .selectAsClass(Video.class,VideoVo.class)
                    .leftJoin(Video.class, Video::getId,Detail::getId)
                    .like(Video::getTitle, keyWord);
        }
        // 测试联表查询
        IPage<VideoVo> iPage= detailMapper.selectJoinPage(new Page<>(pageNo,pageSize),VideoVo.class,
                detailMPJLambdaWrapper);
      Long total = videoMapper.selectCount(queryWrapper);
      iPage.setTotal(Math.toIntExact(total));
      return iPage;
    }

}
